package sigeco.view.backing;

import java.util.List;

import javax.ejb.EJB;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import oracle.adf.view.faces.model.ChildPropertyTreeModel;
import oracle.adf.view.faces.model.TreeModel;

import sigeco.model.Ability;
import sigeco.model.Knowledge;
import sigeco.model.KnowledgeGroup;
import sigeco.model.Matrix;
import sigeco.model.facade.MatrixFacadeBean;
import sigeco.model.facade.MatrixFacadeLocal;

/**
 * Matrix Holder.
 * Represents a managed bean that holds a matrix for editing and creating purposes.
 */
public class MatrixHolder {
    private Matrix _matrix;
    
    private String _newKnowledgeGroupName;
    
    private String _newKnowledgeName;
    private KnowledgeGroup _newKnowledgeHolder;
    
    private String _newAbilityName;
    
    @EJB
    private MatrixFacadeLocal matrixFacade;
    
    public MatrixHolder() {
    }


    public void setMatrix(Matrix matrix) {
        this._matrix = matrix;
    }

    public Matrix getMatrix() {
        if (_matrix == null) {
            _matrix = new Matrix();
        }
        return _matrix;
    }

    public void setNewKnowledgeGroupName(String newKnowledgeGroupName) {
        this._newKnowledgeGroupName = newKnowledgeGroupName;
    }

    public String getNewKnowledgeGroupName() {
        return _newKnowledgeGroupName;
    }

    public void addKnowledgeGroup(ActionEvent actionEvent) {
        if (_newKnowledgeGroupName != null && !_newKnowledgeGroupName.equals("")) {
            KnowledgeGroup kg = new KnowledgeGroup();
            kg.setMatrix(_matrix);
            kg.setTitle(_newKnowledgeGroupName);
            _matrix.getKnowledgeGroups().add(kg);
        }
    }

    public void setNewKnowledgeName(String newKnowledgeName) {
        this._newKnowledgeName = newKnowledgeName;
    }

    public String getNewKnowledgeName() {
        return _newKnowledgeName;
    }

    public void setNewKnowledgeHolder(KnowledgeGroup newKnowledgeHolder) {
        this._newKnowledgeHolder = newKnowledgeHolder;
    }

    public KnowledgeGroup getNewKnowledgeHolder() {
        return _newKnowledgeHolder;
    }

    public void addKnowledge(ActionEvent actionEvent) {
        if (_newKnowledgeName != null && !_newKnowledgeName.equals("") && _newKnowledgeHolder != null) {
            Knowledge knowledge = new Knowledge();
            knowledge.setKnowledgeGroup(_newKnowledgeHolder);
            knowledge.setName(_newKnowledgeName);
            _newKnowledgeHolder.getKnowledges().add(knowledge);
            _newKnowledgeHolder = null;
        }
    }

    public void setNewAbilityName(String newAbilityName) {
        this._newAbilityName = newAbilityName;
    }

    public String getNewAbilityName() {
        return _newAbilityName;
    }

    public void addAbility(ActionEvent actionEvent) {
        if (_newAbilityName != null && !_newAbilityName.equals("")) {
            Ability ability = new Ability();
            ability.setMatrix(_matrix);
            ability.setName(_newAbilityName);
            _matrix.getAbilities().add(ability);
        }
    }
    
    public List<Matrix> getMatrixes() throws NamingException {
        matrixFacade = (MatrixFacadeLocal)new InitialContext().lookup("java:comp/env/ejb/local/MatrixFacade");
        return matrixFacade.queryMatrixFindAll();
    }
}
